package mi.payment.ui.admin;

import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;

import mi.payment.BuildConfig;
import mi.payment.R;
import mi.payment.constants.Configs;
import mi.payment.settings.SystemParamsSettings;
import mi.payment.controller.BaseSystemParameterSettingController;
import mi.payment.controller.SystemParameterSettingController;
import mi.payment.db.PayDetailModel;
import mi.payment.settings.SystemParamsOperation;
import mi.payment.ui.base.ParentActivity;
import mi.payment.utils.MToast;
import sunmi.sunmiui.button.ButtonSwitch;
import sunmi.sunmiui.dialog.DialogCreater;
import sunmi.sunmiui.dialog.EditTextDialog;
import sunmi.sunmiui.dialog.ListDialog;
import sunmi.sunmiui.title.Title;

/**
 * 系统参数设置
 * Created by lj on 2017/1/7.
 */
public class SystemParameterSettingsActivity extends ParentActivity implements View.OnClickListener {

    private TextView mSerialNumberStartingTextView;              //当前流水号
    private TextView mBatchNumberStartTextView;                  //当前批次号
    private TextView set_to_buy_single_signature_styleTextView;  //套打签购单样式
    private TextView correct_number_of_retransmissionsTextView;  //冲正重发次数
    private TextView non_access_switchTextView;                  //非接交易通道开关
    private TextView maximum_transactionTextView;                //最大交易笔数设置
    private TextView alternative_selectionTextView;              //非接选择

    private ButtonSwitch the_transaction_minus_printButtonSwitch;      //撤销类交易打印负号
    private ButtonSwitch whether_to_support_state_secretsButtonSwitch; //是否支持国密
    private ButtonSwitch pre_license_card_numberButtonSwitch;          //是否屏蔽预授权卡号
    private ButtonSwitch electronic_cash_registerButtonSwitch;         //电子现金免签

    private SystemParamsOperation mSystemParamsOperation;
    private EditTextDialog mEditTextDialog;
    private SystemParamsSettings systemParamsSettings;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_system_params_settings);
        mSystemParamsOperation = SystemParamsOperation.getInstance();
        initTitle();
        initView();
        setOnChangeListener();
        updateUI();
    }

    /**
     * 初始化Title
     */
    private void initTitle() {
        Title mTitle;
        mTitle = (Title) findViewById(R.id.system_params_settings_title);
        mTitle.setButtonVisibility(false, true, true, true);
        mTitle.setHeadBgColor(getResources().getColor(R.color.colorBar));
        mTitle.setTitleText(getResources().getString(R.string.system_parameter_setting));
        mTitle.setMenuClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SystemParameterSettingsActivity.this.finish();
            }
        }, null, null, null);
    }


    /**
     * 初始化控件
     */
    public void initView() {
        View
                mSerialNumberStartingView,           //当前流水号
                mBatchNumberStartView,               //当前批次号
                set_to_buy_single_signature_style,   //套打签购单样式
                correct_number_of_retransmissions,   //冲正重发次数
                maximum_transaction,                 //最大交易笔数设置
                the_transaction_minus_print,         //撤销类交易打印负号
                non_access_switch,                   //非接交易通道开关
                whether_to_support_state_secrets,    //是否支持国密
                pre_license_card_number,             //是否屏蔽预授权卡号
                alternative_selection,               //非接选择
                electronic_cash_register;            //电子现金免签
        TextView mTextLeft;
        //流水号起始
        mSerialNumberStartingView = findViewById(R.id.serial_number_starting);
        mTextLeft = (TextView) mSerialNumberStartingView.findViewById(R.id.item_left_text);
        mTextLeft.setText(getResources().getString(R.string.serial_number_starting));
        mSerialNumberStartingTextView = (TextView) mSerialNumberStartingView.findViewById(R.id.item_right_text);

        //批次号开始
        mBatchNumberStartView = findViewById(R.id.batch_number_start);
        mTextLeft = (TextView) mBatchNumberStartView.findViewById(R.id.item_left_text);
        mTextLeft.setText(getResources().getString(R.string.batch_number_start1));
        mBatchNumberStartTextView = (TextView) mBatchNumberStartView.findViewById(R.id.item_right_text);

        the_transaction_minus_print = findViewById(R.id.the_transaction_minus_print);
        mTextLeft = (TextView) the_transaction_minus_print.findViewById(R.id.switch_item_text);
        mTextLeft.setText(getResources().getString(R.string.the_transaction_minus_print));
        the_transaction_minus_printButtonSwitch = (ButtonSwitch) the_transaction_minus_print.findViewById(R.id.set_switch);

        whether_to_support_state_secrets = findViewById(R.id.whether_to_support_state_secrets);
        mTextLeft = (TextView) whether_to_support_state_secrets.findViewById(R.id.switch_item_text);
        mTextLeft.setText(getResources().getString(R.string.whether_to_support_state_secrets));
        whether_to_support_state_secretsButtonSwitch = (ButtonSwitch) whether_to_support_state_secrets.findViewById(R.id.set_switch);

        pre_license_card_number = findViewById(R.id.pre_license_card_number);
        mTextLeft = (TextView) pre_license_card_number.findViewById(R.id.switch_item_text);
        mTextLeft.setText(getResources().getString(R.string.pre_license_card_number));
        pre_license_card_numberButtonSwitch = (ButtonSwitch) pre_license_card_number.findViewById(R.id.set_switch);

//        electronic_cash_register = findViewById(R.id.electronic_cash_register);
//        mTextLeft = (TextView) electronic_cash_register.findViewById(R.id.switch_item_text);
//        mTextLeft.setText(getResources().getString(R.string.electronic_cash_register));
//        electronic_cash_registerButtonSwitch = (ButtonSwitch) electronic_cash_register.findViewById(R.id.set_switch);

        set_to_buy_single_signature_style = findViewById(R.id.set_to_buy_single_signature_style);
        mTextLeft = (TextView) set_to_buy_single_signature_style.findViewById(R.id.item_left_text);
        mTextLeft.setText(getResources().getString(R.string.set_to_buy_single_signature_style));
        set_to_buy_single_signature_styleTextView = (TextView) set_to_buy_single_signature_style.findViewById(R.id.item_right_text);
        set_to_buy_single_signature_style.setVisibility(View.GONE);

        correct_number_of_retransmissions = findViewById(R.id.correct_number_of_retransmissions);
        mTextLeft = (TextView) correct_number_of_retransmissions.findViewById(R.id.item_left_text);
        mTextLeft.setText(getResources().getString(R.string.correct_number_of_retransmissions));
        correct_number_of_retransmissionsTextView = (TextView) correct_number_of_retransmissions.findViewById(R.id.item_right_text);

        non_access_switch = findViewById(R.id.non_access_switch);
        mTextLeft = (TextView) non_access_switch.findViewById(R.id.item_left_text);
        mTextLeft.setText(getResources().getString(R.string.non_access_switch));
        non_access_switchTextView = (TextView) non_access_switch.findViewById(R.id.item_right_text);

        maximum_transaction = findViewById(R.id.maximum_transaction);
        mTextLeft = (TextView) maximum_transaction.findViewById(R.id.item_left_text);
        mTextLeft.setText(getResources().getString(R.string.maximum_transaction));
        maximum_transactionTextView = (TextView) maximum_transaction.findViewById(R.id.item_right_text);

        alternative_selection = findViewById(R.id.alternative_selection);
        mTextLeft = (TextView) alternative_selection.findViewById(R.id.item_left_text);
        mTextLeft.setText(getResources().getString(R.string.alternative_selection));
        alternative_selectionTextView = (TextView) alternative_selection.findViewById(R.id.item_right_text);
        alternative_selection.setVisibility(View.GONE);


        mSerialNumberStartingView.setOnClickListener(this);
        mBatchNumberStartView.setOnClickListener(this);
        set_to_buy_single_signature_style.setOnClickListener(this);//<!-- 套打签购单样式 -->
        correct_number_of_retransmissions.setOnClickListener(this);//<!-- 冲正重发次数 -->
        non_access_switch.setOnClickListener(this);//<!-- 非接交易通道开关 -->
        maximum_transaction.setOnClickListener(this);//<!-- 最大交易笔数设置 -->
        alternative_selection.setOnClickListener(this);//<!-- 非接选择 -->
        the_transaction_minus_print.setOnClickListener(this);//<!-- 撤销类交易打印负号 -->
        whether_to_support_state_secrets.setOnClickListener(this);//<!-- 是否支持国密 -->
        pre_license_card_number.setOnClickListener(this);
        if (!Configs.getInstance().MINUS_PRINT()) {
            the_transaction_minus_print.setVisibility(View.GONE);
        }
        BaseSystemParameterSettingController mBaseSystemParameterSettingController = new SystemParameterSettingController();
        mBaseSystemParameterSettingController.hideView(this);
    }

    private void setOnChangeListener() {
        the_transaction_minus_printButtonSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mSystemParamsOperation.savePrintNegative(isChecked);
            }
        });

        whether_to_support_state_secretsButtonSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mSystemParamsOperation.saveStateSecret(isChecked);
            }
        });

        pre_license_card_numberButtonSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mSystemParamsOperation.saveShieldAuthorizationCard(isChecked);
            }
        });

    }

    public void updateUI() {
        systemParamsSettings = mSystemParamsOperation.getSystemParamsSettings();

        //获取配置参数
        mSerialNumberStartingTextView.setText(systemParamsSettings.getCurrentSerialNum());
        mBatchNumberStartTextView.setText(systemParamsSettings.getBatchNumStart());
        if (systemParamsSettings.getPurchaseOrderStyle() == 0) {
            set_to_buy_single_signature_styleTextView.setText(getResources().getString(R.string.new_sales_slip));
        } else if (systemParamsSettings.getPurchaseOrderStyle() == 1) {
            set_to_buy_single_signature_styleTextView.setText(getResources().getString(R.string.empty_sales_slip));
        } else {
            set_to_buy_single_signature_styleTextView.setText(getResources().getString(R.string.new_sales_slip));
        }
        correct_number_of_retransmissionsTextView.setText(String.valueOf(systemParamsSettings.getCorrectFrequency()));//冲正重发次数
        if (systemParamsSettings.getNonConnection() == 0) {
            non_access_switchTextView.setText(getResources().getString(R.string.non_access_switch1));
        } else if (systemParamsSettings.getNonConnection() == 1) {
            non_access_switchTextView.setText(getResources().getString(R.string.non_access_switch2));
        } else {
            non_access_switchTextView.setText(getResources().getString(R.string.system_online_priority));
        }
        maximum_transactionTextView.setText(String.valueOf(systemParamsSettings.getTransactionNumber()));//最大交易笔数设置
        if (systemParamsSettings.getNonConnectionChoice() == 0) {
            alternative_selectionTextView.setText(getResources().getString(R.string.system_self_adaption));
        } else if (systemParamsSettings.getNonConnectionChoice() == 1) {
            alternative_selectionTextView.setText(getResources().getString(R.string.system_outlay));
        } else if (systemParamsSettings.getNonConnectionChoice() == 2) {
            alternative_selectionTextView.setText(getResources().getString(R.string.system_built_in));
        } else {
            alternative_selectionTextView.setText(getResources().getString(R.string.system_self_adaption));
        }

        the_transaction_minus_printButtonSwitch.setOnCheck(systemParamsSettings.isPrintNegative());   //撤销类交易打印负号
        whether_to_support_state_secretsButtonSwitch.setOnCheck(systemParamsSettings.isStateSecret());//是否支持国密
        pre_license_card_numberButtonSwitch.setOnCheck(systemParamsSettings.isShieldAuthorizationCard());//是否屏蔽预授权卡号
//        electronic_cash_registerButtonSwitch.setOnCheck(systemParamsSettings.iseCashVisaExemption());    //电子现金免签
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.serial_number_starting:
                //设置流水号起始
                if (PayDetailModel.getInstance().getPayDetailCount() > 0) {
                    showToast(getString(R.string.not_update));
                } else {
                    showEditSerialNumDialog();
                }

                break;
            case R.id.batch_number_start:
                //设置批次号开始
                if (PayDetailModel.getInstance().getPayDetailCount() > 0) {
                    showToast(getString(R.string.not_update));
                } else {
                    showEditBatchNumDialog();
                }
                break;
            //套打签购单样式
            case R.id.set_to_buy_single_signature_style:
                set_to_buy_single_signature_style();
                break;
            //冲正重发次数
            case R.id.correct_number_of_retransmissions:
                correct_number_of_retransmissions();
                break;
            //非接交易通道开关
            case R.id.non_access_switch:
                non_access_switch();
                break;
            //最大交易笔数设置
            case R.id.maximum_transaction:
                maximum_transaction();
                break;
            //非接选择
            case R.id.alternative_selection:
                alternative_selection();
                break;
            //撤销类交易打印负号
            case R.id.the_transaction_minus_print:
                the_transaction_minus_printButtonSwitch.setOnCheck(!the_transaction_minus_printButtonSwitch.isChecked());
                break;
            //是否支持国密
            case R.id.whether_to_support_state_secrets:
                whether_to_support_state_secretsButtonSwitch.setOnCheck(!whether_to_support_state_secretsButtonSwitch.isChecked());
                break;
            //是否屏蔽预授权卡号
            case R.id.pre_license_card_number:
                pre_license_card_numberButtonSwitch.setOnCheck(!pre_license_card_numberButtonSwitch.isChecked());
                break;
        }
    }


    private ListDialog listDialog;

    private void set_to_buy_single_signature_style() {
        String[] itemString = new String[]{getResources().getString(R.string.new_sales_slip), getResources().getString(R.string.empty_sales_slip)};
        listDialog = DialogCreater.createListDialog(this, getString(R.string.set_to_buy_single_signature_style), getString(R.string.cancel), itemString);
        listDialog.setItemClickListener(new ListDialog.ItemClickListener() {
            @Override
            public void OnItemClick(int position) {
                mSystemParamsOperation.savePurchaseOrderStyle(position);
                listDialog.dismiss();
                updateUI();
            }
        });
        listDialog.show();
    }

    private void correct_number_of_retransmissions() {
        mEditTextDialog = DialogCreater.createEditTextDialog(this, getString(R.string.cancel),
                getString(R.string.save), getString(R.string.correct_number_of_retransmissions), new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mEditTextDialog.dismiss();
                    }
                }, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (TextUtils.isEmpty(mEditTextDialog.getText())) {
                            showToast(getString(R.string.error_input));
                        } else {
                            if (checkEmpty(mEditTextDialog.getText()) && checkInt(mEditTextDialog.getText(), 3, 1)) {
                                int num = Integer.parseInt(mEditTextDialog.getText());
                                mSystemParamsOperation.saveCorrectFrequency(num);
                                mEditTextDialog.dismiss();
                                updateUI();

                            }
                        }
                    }
                }, new DialogInterface.OnShowListener() {
                    @Override
                    public void onShow(DialogInterface dialog) {
                        mEditTextDialog.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
                        mEditTextDialog.setMaxLength(1);
                        showSoftKeyboard(mEditTextDialog);
                    }
                });
        mEditTextDialog.setText(systemParamsSettings.getCorrectFrequency() + "");
        mEditTextDialog.show();

    }

    private void non_access_switch() {
        String[] itemString = new String[]{getResources().getString(R.string.system_online_priority), getResources().getString(R.string.system_ecash_priority)};
        listDialog = DialogCreater.createListDialog(this, getString(R.string.non_access_switch), getString(R.string.cancel), itemString);
        listDialog.setItemClickListener(new ListDialog.ItemClickListener() {
            @Override
            public void OnItemClick(int position) {
                mSystemParamsOperation.saveNonConnection(position);
                listDialog.dismiss();
                updateUI();
            }
        });
        listDialog.show();
    }

    private void maximum_transaction() {
        mEditTextDialog = DialogCreater.createEditTextDialog(this, getString(R.string.cancel),
                getString(R.string.save), getString(R.string.maximum_transaction), new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mEditTextDialog.dismiss();
                    }
                }, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (TextUtils.isEmpty(mEditTextDialog.getText())) {
                            showToast(getString(R.string.error_input));
                        } else {
                            if (checkEmpty(mEditTextDialog.getText())) {
                                int num = Integer.parseInt(mEditTextDialog.getText());
                                if (num > SystemParamsSettings.MAX_TRANSACTION_NUMBER || num < 1) {
                                    MToast.showToast(getResources().getString(R.string.system_maxcount), Toast.LENGTH_LONG);
                                    return;
                                }
                                mSystemParamsOperation.saveTransactionNumber(num);
                                mEditTextDialog.dismiss();
                                updateUI();
                            }
                        }
                    }
                }, new DialogInterface.OnShowListener() {
                    @Override
                    public void onShow(DialogInterface dialog) {
                        mEditTextDialog.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
                        mEditTextDialog.setMaxLength(4);
                        showSoftKeyboard(mEditTextDialog);
                    }
                });
        mEditTextDialog.setText(systemParamsSettings.getTransactionNumber() + "");
        mEditTextDialog.show();
    }

    private void alternative_selection() {
        String[] itemString = new String[]{getResources().getString(R.string.system_self_adaption), getResources().getString(R.string.system_outlay), getResources().getString(R.string.system_built_in)};
        listDialog = DialogCreater.createListDialog(this, getString(R.string.alternative_selection), getString(R.string.cancel), itemString);
        listDialog.setItemClickListener(new ListDialog.ItemClickListener() {
            @Override
            public void OnItemClick(int position) {
                mSystemParamsOperation.saveNonConnectionChoice(position);
                listDialog.dismiss();
                updateUI();
            }
        });
        listDialog.show();
    }

    /**
     * 显示流水号开始
     */
    private void showEditSerialNumDialog() {
        SystemParamsSettings systemParamsSettings = mSystemParamsOperation.getSystemParamsSettings();
        mEditTextDialog = DialogCreater.createEditTextDialog(this, getString(R.string.cancel), getString(R.string.save), getString(R.string.serial_number_starting1), new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mEditTextDialog.dismiss();
            }
        }, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (checkEmpty(mEditTextDialog.getText()) && (mEditTextDialog.getText().length() == 6)) {
                    if (Integer.parseInt(mEditTextDialog.getText()) < 1) {
                        mSystemParamsOperation.saveSerialNumber("000001");
                    } else {
                        mSystemParamsOperation.saveSerialNumber(mEditTextDialog.getText());
                    }
                    mEditTextDialog.dismiss();
                    updateUI();
                } else {
                    showToast(getString(R.string.error_input));
                }
            }
        }, new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                mEditTextDialog.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
                showSoftKeyboard(mEditTextDialog);
                mEditTextDialog.setMaxLength(6);
            }
        });
        if (TextUtils.isEmpty(systemParamsSettings.getCurrentSerialNum())) {
            mEditTextDialog.setHintText(getResources().getString(R.string.return_unknow));
        } else {
            mEditTextDialog.setText(systemParamsSettings.getCurrentSerialNum());
        }
        mEditTextDialog.show();

    }

    /**
     * 批次号开始
     */
    private void showEditBatchNumDialog() {
        mEditTextDialog = DialogCreater.createEditTextDialog(this, getString(R.string.cancel),
                getString(R.string.save), getString(R.string.batch_number_start1),
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mEditTextDialog.dismiss();
                    }
                }, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (checkEmpty(mEditTextDialog.getText()) && (mEditTextDialog.getText().length() == 6)) {
                            if (Integer.parseInt(mEditTextDialog.getText()) < 1) {
                                mSystemParamsOperation.saveBatchNumber("000001");
                            } else {
                                mSystemParamsOperation.saveBatchNumber(mEditTextDialog.getText());
                            }
                            mEditTextDialog.dismiss();
                            updateUI();
                        } else {
                            showToast(getString(R.string.error_input));
                        }
                    }
                }, new DialogInterface.OnShowListener() {
                    @Override
                    public void onShow(DialogInterface dialog) {
                        mEditTextDialog.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
                        showSoftKeyboard(mEditTextDialog);
                        mEditTextDialog.setMaxLength(6);
                        mEditTextDialog.setText(mBatchNumberStartTextView.getText().toString());
                    }
                });
        mEditTextDialog.show();
    }
}
